package com.lanou.myim.ui.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.R;
import com.lanou.myim.tools.CustomDialog;
import com.lanou.myim.util.BarcodeFormat;
import com.lanou.myim.util.Result;
import com.lanou.myim.util.ZBarScannerView;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * Created by dllo on 16/3/10.
 */
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private static final String TAG = "ScanActivity";
    private ZXingScannerView scanView;
    private String userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // API23之后需要手动调用获取权限
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
//                != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA}, 1);
        userName = EMChatManager.getInstance().getCurrentUser();
//        }
        scanView = new ZXingScannerView(this);
        setContentView(scanView);

    }

    @Override
    public void onResume() {
        super.onResume();
        scanView.setResultHandler(this);
        scanView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        scanView.stopCamera();
    }

    private Handler mHandler = new Handler();

    // 扫描成功后会回调此方法
    @Override
    public void handleResult(com.google.zxing.Result result) {
        Log.v(TAG, result.getText());
        Log.v(TAG, result.getBarcodeFormat().toString());
   dialog1_1(result.getText());
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                scanView.resumeCameraPreview(ScannerActivity.this);

            }
        }, 2000);
    }

    private void dialog1_1(final String friendName) {

        final String reason = "我是" + userName;
        final CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("是否加" + friendName + "为好友");
        builder.setTitle("添加好友");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                try {
                    EMContactManager.getInstance().addContact(friendName, reason);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                    Toast.makeText(ScannerActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
                //设置你的操作事项
            }
        });

        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create().show();
    }


}
